package leetcode.easy.q21;

/**
 * 合并两个有序链表
 * Q21: { @link https://leetcode-cn.com/problems/merge-two-sorted-lists}
 */
public class Q {
    public static void main(String[] args) {
        ListNode node = new Solution().mergeTwoLists(fromArr(1, 2, 4), fromArr(1, 3, 4));
        System.out.printf(print(node));
    }

    private static ListNode fromArr(int... arr) {
        if (arr == null) return null;
        ListNode node = new ListNode(arr[0]), temp = node;
        for (int i = 1; i < arr.length; i++) {
            temp.next = new ListNode(arr[i]);
            temp = temp.next;
        }
        return node;
    }

    private static String print(ListNode node) {
        if (node == null) return "";
        String val = "";
        while (node != null) {
            val += node.val + ",";
            node = node.next;
        }
        return "[" + val.substring(0, val.length() - 1) + "]";
    }

}


/**
 * 不损坏原有List,构造新的list
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode n1 = l1, n2 = l2, current = null;
        ListNode parent = new ListNode();
        current = parent;
        while (n1 != null || n2 != null) {
            if (n1 == null) {
                current.next = new ListNode(n2.val);
                current = current.next;
                n2 = n2.next;
                continue;
            }
            if (n2 == null) {
                current.next = new ListNode(n1.val);
                current = current.next;
                n1 = n1.next;
                continue;
            }
            if (n2.val <= n1.val) {
                current.next = new ListNode(n2.val);
                current = current.next;
                n2 = n2.next;
            } else {
                current.next = new ListNode(n1.val);
                current = current.next;
                n1 = n1.next;
            }
        }
        return parent.next;
    }

}

/**
 * 利用原有节点直接merge
 */
class Solution2 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode res = new ListNode(), temp = res;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                temp.next = l1;
                l1 = l1.next;
                temp = temp.next;
            } else {
                temp.next = l2;
                l2 = l2.next;
                temp = temp.next;
            }
        }
        if (l1 == null) temp.next = l2;
        if (l2 == null) temp.next = l1;
        return res.next;
    }
}

/**
 * 利用原有节点直接merge---递归
 */
class Solution3 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l2.val <= l1.val) {
            l2.next = mergeTwoLists(l2.next, l1);
            return l2;
        } else {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        }
    }
}